/* 
 * File:   disaster.cpp
 * Author: dolphinigle
 * 
 * Created on March 16, 2011, 11:33 AM
 */

#include <assert.h>
#include <algorithm>
#include "disaster.hpp"
#include "gridmap.hpp"
#include "creatures.hpp"
#include "gamemaps.hpp"

const int Disaster::kDisasterCount = 4;
const int Disaster::kDisasterChance = 90;
const int Disaster::kDisasterWeight[] = {
  5,
  10,
  10,
  10,
  10};
const bool Disaster::kDisasterActive[] = {
  true,
  true,
  true,
  true,
  true};
const std::string Disaster::kDisasterName[] = {
  "Sidang Anggota",
  "ITBPC",
  "Project Storm",
  "Inferno",
  "PLO", // TODO
  "Sparta Tahap 1", // TODO
  "Sparta Tahap 2", // TODO
  "Arkavidia"
};
const std::string Disaster::kDisasterDescription[] = {
  "It's an armageddon! In the midst of project storm comes Sidang Anggota!\n"
  "All of your plants have died.",
  "Yay! Finally ITBPC has arrived!\n"
  "You received protection from disasters in the next 3 days.",
  "Project storm has struct your farm.\n"
  "All of your not-dead plants has returned to seed form.",
  "INFERNO! Burn your plants and spirit!\n"
  "All of the plantable cells are burning (water them to cease the fire)",
  "All of your sprites are forced to attend PLO!\n"
  "Your sprites are unable to help in the next 3 days."
};

Disaster::Disaster(GameInterface* game) : game_(game),
    itbpc_protection_duration_(0) {
  srand(time(0));

}

// load game constructor
Disaster::Disaster(GameInterface* game, std::ifstream& is) : game_(game),
    itbpc_protection_duration_(0) {
  srand(time(0));
  is >> itbpc_protection_duration_;
}

void Disaster::save(std::ofstream& os) {
  os << itbpc_protection_duration_ << std::endl;
}

Disaster::~Disaster() {
}

void Disaster::showData() {
  // TODO
}

void Disaster::updateDisaster() {
  if (itbpc_protection_duration_ > 0) {
    game_->getView()->showString("The light of ITBPC shines upon and protects us from calamity!\n");
    if (itbpc_protection_duration_ > 1) {
      game_->getView()->showString("ITPBC is still ongoing for ");
      game_->getView()->showInt(itbpc_protection_duration_-1);
      game_->getView()->showString(" days!\n");
    } else {
      game_->getView()->showString("Oh no! ITBPC has just ended!\n");
    }
  }
}

void Disaster::showInfo(int disaster_id) {
  // DEPRECATED?
}

void Disaster::addProtection(int day_count) {
  itbpc_protection_duration_ += day_count;
}

void Disaster::performDisaster(int disaster_id) {
  switch (disaster_id) {
    case 0 :
      // Sidang Anggota
      game_->getCreatures()->getPlants()->makeAllDead();
      break;
    case 1 :
      // ITBPC
      itbpc_protection_duration_ = 3;
      break;
    case 2 :
      // Badai Tubes
      game_->getCreatures()->getPlants()->restartAllNotDeadToSeed();
      break;
    case 3 :
      // INFERNO
      game_->getMaps()->getField()->burnAllPlantableCells();
      break;
    case 4 :
      // TODO
      assert(false);
  }


  game_->getView()->showDisaster(kDisasterName[disaster_id],
              kDisasterDescription[disaster_id]);
}

void Disaster::advanceDay() {

  updateDisaster();

  int total_weight = 0;
  for (int i = 0; i < kDisasterCount; ++i) {
    if (kDisasterActive[i]) {
      total_weight += kDisasterWeight[i];
    }
  }

  int random_val = rand() % 100;

  if (itbpc_protection_duration_ > 0) {
    random_val = 100;
    --itbpc_protection_duration_;
  }

  if (random_val < kDisasterChance) {
    // disaster happens
    // picks one
    int random_val2 = rand() % total_weight;
    for (int i = 0; i < kDisasterCount; ++i) {
      if (kDisasterActive[i]) {
        if (random_val2 < kDisasterWeight[i]) {
          performDisaster(i);
          break;
        } else {
          random_val2 -= kDisasterWeight[i];
        }
      }
    }
  }

}

void Disaster::advanceTime() {
  // TODO
}



